package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.OrderDetailBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.CallPhoneUtil;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.StringUtil;
import com.dlwx.parttimetalent.utils.ToChatUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderDetailActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.image_header)
    ImageView imageHeader;
    @BindView(R.id.order_type)
    TextView TvOrderType;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_delete)
    TextView tvDelete;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_onder_num)
    TextView tvOnderNum;
    @BindView(R.id.tv_onder_time)
    TextView tvOnderTime;
    @BindView(R.id.ll_chat)
    LinearLayout llChat;
    @BindView(R.id.ll_phone)
    LinearLayout llPhone;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_start)
    TextView tvStart;
    @BindView(R.id.tv_end)
    TextView tvEnd;
    @BindView(R.id.tv_duration)
    TextView tvDuration;
    @BindView(R.id.tv_where)
    TextView tvWhere;
    @BindView(R.id.tv_auth)
    TextView tvAuth;
    @BindView(R.id.ll_location)
    LinearLayout llLocation;
    private String orderId;
    private String orderType;
    private String curruserId;
    private String headUrl;
    private String chatName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        ButterKnife.bind(this);
        initView();
        getDatas();
    }

    private void initView() {
        Intent i = getIntent();
        orderId = i.getStringExtra("id");
        orderType = i.getStringExtra("orderType");
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("订单详情");
        tvMiddleTitle.setVisibility(View.VISIBLE);
    }

    private void getDatas() {
        OkgoUtil.post(Http.OrderDetails, OrderDetailActivity.this)
                .params("orderType", orderType)
                .params("orderid", orderId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        OrderDetailBean parentBean = DealResult.getInstance().dealDatas(OrderDetailActivity.this
                                , response, new TypeToken<OrderDetailBean>() {
                                }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        int code = parentBean.getCode();
                        if (code == Constant.SUCESS) {
                            setDatas(parentBean.getBody());
                        }
//                        ToastUtils.toastForShort(OrderDetailActivity.this,parentBean.getResult());

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.toastForShort(OrderDetailActivity.this, getString(R.string.bad_net));
                    }
                });
    }

    OrderDetailBean.BodyBean bean;

    private void setDatas(OrderDetailBean.BodyBean bean) {
        if (bean != null) {
            this.bean = bean;
            curruserId = bean.getCurruserid() + "";
            headUrl = bean.getHeadurl();
            Glide.with(this).load(bean.getHeadurl()).into(imageHeader);
            TvOrderType.setText("雇佣类型:" + bean.getLabelname());
            tvMoney.setText("合计：" + getString(R.string.rmb) + bean.getOrderTotal());
            tvOnderNum.setText("订单编号：" + bean.getOrderno());
            tvOnderTime.setText("成交时间：" + bean.getTrantime());
            tvStart.setText("开始时间：" + bean.getStartdate() + " " + bean.getStarttime());
            tvEnd.setText("结束时间：" + bean.getEnddate() + " " + bean.getEndtime());
            tvDuration.setText(bean.getWhentime());
            tvWhere.setText("工作位置：" + bean.getOrder_area());
            if (bean.getAuthname() == 1) {
                tvAuth.setText("已实名认证");
                tvAuth.getPaint().setFakeBoldText(false);
                tvAuth.setTextColor(ContextCompat.getColor(this, R.color.order_type_green));
            } else {
                tvAuth.setTextColor(ContextCompat.getColor(this, R.color.colorAccent));
                tvAuth.getPaint().setFakeBoldText(true);
                tvAuth.setText("未实名认证");
            }
            tvPhone.setText(bean.getPhone_no());
            String remarkName = bean.getRemarkName();
            if(TextUtils.equals(bean.getStatusText(),"已接受")){
                llLocation.setVisibility(View.VISIBLE);
            }else{
                llLocation.setVisibility(View.INVISIBLE);

            }
            if (TextUtils.equals("1", orderType)) {

                tvMoney.setTextColor(ContextCompat.getColor(this, R.color.order_type_red));
                if (TextUtils.isEmpty(remarkName)) {
                    chatName = StringUtil.subName(bean.getNickname());
                    tvName.setText("兼职人:" + StringUtil.subName(bean.getNickname()));
                } else {
                    chatName = StringUtil.subName(remarkName);
                    tvName.setText("兼职人:" + StringUtil.subName(remarkName));
                }
                if (TextUtils.isEmpty(bean.getStarttime())) {
                    tvTime.setText("兼职时间：" + bean.getStartdate() + "~" + bean.getEnddate());
                } else {
                    tvTime.setText("兼职时间：" + bean.getStartdate() + "~" + bean.getEnddate() + "     " + bean.getStarttime() + "~" + bean.getEndtime());
                }
                tvAddress.setText("兼职人位置：");
            } else {
                tvMoney.setTextColor(ContextCompat.getColor(this, R.color.order_type_green));
                if (TextUtils.isEmpty(remarkName)) {
                    chatName = StringUtil.subName(bean.getNickname());
                    tvName.setText("下单人:" + StringUtil.subName(bean.getNickname()));
                } else {
                    chatName = StringUtil.subName(remarkName);
                    tvName.setText("下单人:" + StringUtil.subName(remarkName));
                }
                if (TextUtils.isEmpty(bean.getStarttime())) {
                    tvTime.setText("下单时间：" + bean.getStartdate() + "~" + bean.getEnddate());
                } else {
                    tvTime.setText("下单时间：" + bean.getStartdate() + "~" + bean.getEnddate() + "     " + bean.getStarttime() + "~" + bean.getEndtime());
                }
                tvAddress.setText("下单人位置：");
            }
        }
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_location, R.id.image_header, R.id.tv_cancel, R.id.ll_chat, R.id.ll_phone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_location:
                Double lat = null, lon = null;
                try {
                    if (!TextUtils.isEmpty(bean.getLat())) {
                        lat = Double.parseDouble(bean.getLat());
                    }
                    ;
                    if (!TextUtils.isEmpty(bean.getLongitude())) {
                        lon = Double.parseDouble(bean.getLongitude());
                    }
                    ;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Intent i = new Intent(this, MapActivity.class);
                i.putExtra("lon", lon);
                i.putExtra("lat", lat);
                startActivity(i);
                break;
            case R.id.image_header:
                Intent intent = new Intent(this, AttentionUser2Activity.class);
                intent.putExtra("listId", curruserId);
                startActivity(intent);
                break;
            case R.id.tv_cancel:
                break;
            case R.id.ll_chat:
                ToChatUtil.chat(this, curruserId, headUrl, chatName);
                break;
            case R.id.ll_phone:
                CallPhoneUtil.call(this, tvPhone.getText().toString());
                break;
        }
    }
}
